<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class EffectController extends Controller
{
	//列表页
    public function getIndex(Request $request)
    {
    	if($request->input('keywords')){
    		$cate = DB::table('effect')
    		->where('name','like','%'.$request->input('keywords').'%')
    		->paginate(5);
    	}else{
        	$cate = DB::table('effect')
    		->where('name','like','%'.$request->input('keywords').'%')
    		->paginate(5);
    	}
    	$data = $request->all();
    	return view('admin.effect.index',['cate'=>$cate,'data'=>$data]);
    }
    //添加
    public function getAdd()
    {
    	return view('admin.effect.add');
    }
    //执行添加
    public function postInsert(Request $request)
    {
    	$data['name'] = $request->input('name');	
    	$res = DB::table('effect')->insert($data);
    	if($res){
			return redirect('/admin/effect/index')->with('success','添加成功');
		}else{
			return back();
		}

	}
	//删除
	public function getDelete($id)
	{
		$r = DB::table('effect')->where('id',$id)->delete();
		if($r){
			return back()->with('success','删除成功');
		}else{
			return back()->with('error','删除失败');
		}
	}
	//修改
	public function getEdit($id)
	{
		$cate = DB::table('effect')->where('id',$id)->first();
		return view('admin.effect.edit',['cate'=>$cate]);
	}
	//执行修改
	public function postUpdate(Request $request)
	{

    	$id = $request->input('id');
    	$data['name'] = $request->input('name');
    	
    	$res = DB::table('effect')->where('id',$id)->update($data);
	    if($res){
			return redirect('/admin/effect/index')->with('success','修改成功');
	    }else{
			return back()->withInput();
	    }
	}
}
